<?php

namespace app\common\model;
use think\Db;
use think\exception\HttpException;
class Channel
{
    private $channelId;//当前渠道id
    private $cpsId;//当前cps id
    public function __construct()
    {
    }

    public function getChannel($channelId)
    {
        if(!empty($channelId)&&is_numeric($channelId)){
            $keyId='channel:id:'.$channelId;
            $redis=new Redis();
            $channel=$redis->hGetAll($keyId);
            if(empty($channel)){
                $ttl=config('channel_ttl');
                if($channelId==1000){
                    $channel=Db::name('channel')->where(['status'=>1,'channel_id'=>$channelId,'cps_id'=>1000])->find();
                }else{
                    $channel=Db::name('channel')->where(['status'=>1,'channel_id'=>$channelId])->find();
                }

                if(!empty($channel)){
                    $redis->hMset($keyId,$channel);
                    $redis->expire($keyId,$ttl);
                    $keyName='channel:name:'.$channel['channel_name'];
                    $redis->set($keyName,$channelId,$ttl);
                }else{
                    return false;
                }
            }
            return $channel;
        }else{
            return false;
        }
    }

    public function getCps($cpsId,$channelId=1000)
    {
        if(!empty($cpsId)&&is_numeric($cpsId)){
            $keyId='channel:id:1000:'.$cpsId;
            $redis=new Redis();
            if($redis->exists($keyId)){
                return $redis->hGetAll($keyId);
            }else{
                $ttl=config('channel_ttl');
                $channel=Db::name('channel')->where(['status'=>1,'channel_id'=>$channelId,'cps_id'=>$cpsId])->find();
                if(!empty($channel)){
                    $redis->hMset($keyId,$channel);
                    $redis->expire($keyId,$ttl);
                    return $channel;
                }else{
                    return [];
                }
            }
        }else{
            return [];
        }
    }

    /**
     * 获取渠道id
     * @param $channelName
     * @return bool|mixed
     */
    public function getChannelId($channelName){
        if(empty($channelName)){
            return false;
        }
        $redis=new Redis();
        $keyName='channel:name:'.$channelName;
        $channelId=$redis->get($keyName);
        if($channelId===false){
            $ttl=config('channel_ttl');
            $channel=Db::name('channel')
                ->where(['code'=>$channelName])
                ->find();
            if(empty($channel)){
                return false;
            }else{
                $keyId='channel:id:'.$channel['channel_id'];
                $redis->hMset($keyId,$channel);
                $redis->expire($keyId,$ttl);
                $redis->set($keyName,$channel['channel_id'],$ttl);
                return $channel['channel_id'];
            }
        }else{
            return $channelId;
        }
    }

    /**
     * 获取渠道游戏配置
     * @param $gameId
     * @param $channelId
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getChannelGameConf($gameId,$channelId){
        if(empty($gameId)||empty($channelId)){
            return false;
        }
        $key='channel:conf:'.$channelId.':'.$gameId;
        $redis=new Redis();
        $cgc=$redis->hGetAll($key);
        if(empty($cgc)){
            $cgc=Db::name('channel_game_conf')
                ->where(['channel_id'=>$channelId,'game_id'=>$gameId])
                ->find();
            if(empty($cgc)){
                return false;
            }else{
                $redis->hMset($key,$cgc);
                $redis->expire($key,config('channel_ttl'));
                return $cgc;
            }
        }else{
            return $cgc;
        }
    }

    public function checkIp($channelId,$ip){
        $channel=$this->getChannel($channelId);
        if(empty($channel))throw new HttpException(200,'渠道不存在:'.$channelId);
        $ipList=$channel['server_ip'];
        if($ipList==='unlimited'){
            return true;
        }
        if(strpos($ipList,$ip)!==false){
            return true;
        }else{
            return false;
        }
    }

    public function getPayType($channelId){
        $channel=$this->getChannel($channelId);
        if($channel){
            return isset($channel['pay_type'])?$channel['pay_type']:null;
        }

    }


}



